package net.alan.modules.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.alan.modules.bean.Feedback;
import net.alan.modules.bean.PageModel;
import net.alan.modules.bean.Result;
import net.alan.modules.bean.ResultUtil;
import net.alan.modules.cache.WebContextUtil;
import net.alan.modules.handle.BaseExecption;
import net.alan.modules.service.IFeedbackService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/21
 */
@Api(tags = "用户反馈管理")
@Controller
@RequestMapping(value = "feedback")
public class FeedbackController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(FeedbackController.class);
    @Autowired
    private IFeedbackService feedbackService;


    @ApiOperation(value = "添加", notes = "")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(Feedback feedback) throws Exception {
        try {
            feedback.setUser(WebContextUtil.getCurrentUser().getUser());
            feedbackService.save(Feedback.class,feedback);
        } catch (BaseExecption baseExecption) {
            return ResultUtil.error("保存异常");
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除", notes = "")
    @ApiImplicitParam(name = "id", value = "Id", required = true, dataType = "Integer")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable("id") Integer id) throws Exception {
        Result result = null;
        try {
            result = feedbackService.delete(Feedback.class, id);
        } catch (BaseExecption baseExecption) {
            result = ResultUtil.error("删除异常");
        }
        return result;
    }

    @ApiOperation(value = "获取一个", notes = "")
    @ApiImplicitParam(name = "id", value = "Id", required = true, dataType = "Integer")
    @RequestMapping(value = "get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Feedback get(@PathVariable("id") Integer id) throws Exception {
        return feedbackService.getById(Feedback.class, id).copy();
    }

    @ApiOperation(value = "列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public PageModel list(PageModel pageModel) throws Exception {
        Page<Feedback> list = this.feedbackService.findAll(Feedback.class, pageModel);
        return pageModel.toPageModel(list);
    }
}
